/*
 * @(#) $Header$
 *
 * Copyright (C) 2007  Forklabs Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.media.jai.opimage;

import java.awt.RenderingHints;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.ContextualRenderedImageFactory;
import java.awt.image.renderable.ParameterBlock;
import javax.media.jai.CollectionImage;
import ca.forklabs.media.jai.operator.PipelineDescriptor;
import ca.forklabs.media.jai.operator.SpectralFilterDescriptor;
import ca.forklabs.media.jai.FormatDataType;
import ca.forklabs.media.jai.ParameterBlockUtil;
import ca.forklabs.media.jai.SpectralFilter2D;
import ca.forklabs.media.jai.operator.SpectralHomomorphicDescriptor;

/**
 * Class {@code SpectralHomomorphicCRIF} is a
 * {@link ContextualRenderedImageFactory} supporting the
 * <em>spectralhomomorphic</em> operation in the rendered and renderable image
 * layers.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.media.jai.operator.SpectralHomomorphicCRIF">Daniel Léonard</a>
 * @version $Revision$
 */
public class SpectralHomomorphicCRIF extends AbstractCRIF {

//---------------------------
// Constructor
//---------------------------

   /**
    * Constructor.
    */
   public SpectralHomomorphicCRIF() {
      super(SpectralHomomorphicDescriptor.NAME);
      }


//---------------------------
// Instance methods
//---------------------------

   /**
    * Gets the spectral filter.
    * @param   pb   the parameter block.
    * @return   the spectral filter.
    */
   protected SpectralFilter2D getFilter(ParameterBlock pb) {
      int index = SpectralHomomorphicDescriptor.FILTER_PARAMETER_INDEX;
      SpectralFilter2D filter = (SpectralFilter2D) pb.getObjectParameter(index);
      return filter;
      }

   /**
    * Gets the type for the format operation.
    * @param   pb   the parameter block.
    * @return   type for the format operation.
    */
   protected FormatDataType getDataType(ParameterBlock pb) {
      int index = SpectralHomomorphicDescriptor.TYPE_PARAMETER_INDEX;
      FormatDataType type = (FormatDataType) pb.getObjectParameter(index);
      return type;
      }


//---------------------------
// Implemented methods from javax.media.jai.CRIFImpl
//---------------------------

   /**
    * Enhance the image.
    * @param   pb   the parameter block.
    * @param   hints   optional rendering hints.
    */
   @Override
   @SuppressWarnings("nls")
   public RenderedImage create(ParameterBlock pb, RenderingHints hints) {
      CollectionImage sources = (CollectionImage) pb.getSource(0);
      RenderedImage source = (RenderedImage) sources.get(0);

      SpectralFilter2D filter = this.getFilter(pb);
      FormatDataType type = this.getDataType(pb);

      String[] operations = new String[] {
         "format",
         "addconst",
         "log",
         SpectralFilterDescriptor.NAME,
         "exp",
         "subtractconst",
         };

      ParameterBlock[] pbs = new ParameterBlock[] {
          ParameterBlockUtil.createFormatParameterBlock(null, type),
          ParameterBlockUtil.createAddConstParameterBlock(null, new double[] { 1.0, }),
          ParameterBlockUtil.createLogParameterBlock(null),
          new ParameterBlock().add(filter).add(type),
          ParameterBlockUtil.createExpParameterBlock(null),
          ParameterBlockUtil.createSubtractConstParameterBlock(null, new double[] { 1.0, }),
         };
      RenderedImage sink = PipelineDescriptor.create(source, operations, pbs, hints);
      return sink;
      }

// TODO : is the create method for renderable necessary ?

   }
